
//NOTE: Array & Slice
// [T; length] => this is array;
// [T] => this is slice
// let array: [i32; 5] = [1, 2, 3, 4, 5];
//  1. mem::size_of_val(&array);
//
//  2. function call :
//      declare:  fn max(slice: &[i32]) -> Option<i32>;
//      use : convert &array to slice  
//              max(&array);   
//              max(&[1, 2, 3, 4, 5]); 

fn max<T>(array: &[T]) -> Option<T> where 
    T: std::cmp::PartialOrd + std::marker::Copy {
    if array.len() == 0 {
        return None;
    }

    let mut result: T = array[0];
    for i in 1.. array.len() { 
        if result < array[i] {
            result = array[i];
        }
    }
    Some(result)
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_max(){
        //Normal 
        assert_eq!(Some(3), max(&[1_i32, 2, 3]));
        assert_eq!(Some(4), max(&[1_i32, 4, 3]));
        assert_eq!(Some(4), max(&[4_i32, 4, 4]));
        //Edge test 
        assert_eq!(Some(1), max(&[1_i32]));
        //Exception Test
        assert_eq!(None, max::<i32>(&[])); // normal case 
    }
}
